package org.uim.cricket.afetcher;

import org.uim.cricket.afetcher.exec.AnalyzerExecutable;

public class AnalyzerFetcher {

	static {
		System.setProperty("org.apache.commons.logging.Log",
        "org.apache.commons.logging.impl.NoOpLog");
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		AFlogger.printHeader(" Starting Analyzer Fetcher application.");
		
		if (args==null || args.length == 0) {
			AFlogger.error("Arguments for [AnalyzerFetcher] can't be empty!");
			AFlogger.debug(" Terminating application.");
			throw new IllegalArgumentException("Arguments for [AnalyzerFetcher] can't be empty!");
		}
		
		AFlogger.debug(" Loading analyzer properties...");
		FetcherProperties prop = new FetcherProperties();
		prop.load();
		AFlogger.debug(" ok.");
		
		AFlogger.debug(" Loading analyzer fetcher beans...");
		AFetcherBeans beans = new AFetcherBeans("../config/executor-beans.xml");
		beans.loadBeans();
		AFlogger.debug("  Completed loading beans.");
		
		String theBean = "default-analyzerExecutor";
		AFlogger.debug(" fetching corresponding executor bean... ["+theBean+"]");
		AnalyzerExecutable aexec = beans.getExecutorBean(theBean);
		
		int total = 0;
		int success = 0;
		
		try {
			
			AFlogger.printSubHeader(" Initializing analyzer executor...");
			AFlogger.debug(" repository path: " + args[0]);
			aexec.setRepositoryPath(args[0]);
			
			aexec.runAnalyzer(prop);
			total += aexec.getTotalAnalyzedFiles();
			success += aexec.getSuccessAnalyzedFiles();
			
		} catch (Exception ex) {
			AFlogger.error(" ERROR ocuured!", ex);
			AFlogger.debug(" Terminating Analyzer Fetcher.");
			throw new Exception("Error in Analyzer fetcher!", ex);
		}
		
		if (total != success) {
			AFlogger.warn(" " + success + " out of " + total + " has been successfully finished!"); 
		} else {
			AFlogger.debug(" All files successfully completed Analyzer Fetcher!");
		}
		AFlogger.debug(" Application exit.");
	}

	
}
